<?php

namespace App\Model;


use Illuminate\Support\Facades\DB;

class Prop extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'prop';
    protected $primaryKey = 'prop_id';
    protected $fields = ['prop_id', 'name'];

    /**
     * 创建或更新
     * prop_id 存在则更新，不存在则新增
     * @param $data
     * @return mixed
     */
    public function createOrUpdateData($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/prop/upsert', $data, 'json', 'POST');
    }

    public function propEvent()
    {
        return $this->hasMany(PropEvent::class, 'prop_id', 'prop_id');
    }

    public function getAllList()
    {
        return DB::connection($this->connection)->table($this->table)->orderBy('prop_id', 'desc')->get();
    }

    public function getPropInfoByIds($propIds)
    {
        $row = DB::connection('api')->table($this->table)->whereIn('prop_id', $propIds)->get(['prop_id', 'name'])->toArray();
        return $this->objToArray($row);
    }

    public function getProps($request)
    {
        $page = $request->get('page', 0);
        $limit = $request->get('limit', 10);

        return DB::connection('api')->table($this->table)
            ->orderBy('prop_id', 'desc')
            ->paginate($limit, $this->fields, 'page', $page);
    }
}
